package com.linked.store.label.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.store.box.bean.param.AddBoxInfoParam;
import com.linked.store.label.bean.param.AddLabelInfoParam;
import com.linked.store.label.bean.param.PasteLabelParam;
import com.linked.store.label.service.ILabelHandleService;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :dbq
 * @date : 2023/3/23 15:09
 * @description : desc
 */
@RestController
@RequestMapping("labelhandle")
@Slf4j
public class LabelHandleController {

    private final ILabelHandleService labelHandleService;

    private final ObjectMapper mapper;

    @Autowired
    public LabelHandleController(ILabelHandleService labelHandleService, ObjectMapper mapper) {
        this.labelHandleService = labelHandleService;
        this.mapper = mapper;
    }

    @PostMapping("addLabelInfo")
    Result addLabelInfo(@RequestBody AddLabelInfoParam param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("添加标签接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = labelHandleService.addLabelInfo(param);
        } catch (Exception e) {
            log.error("添加标签接口 异常", e);
            return Result.error("出现未知错误，请联系管理员");
        }
        return ret ? Result.success() : Result.ok(false, "添加失败！");
    }

    @PostMapping("pasteLabel")
    Result pasteLabel(@RequestBody PasteLabelParam param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("粘贴标签接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = labelHandleService.pasteLabel(param);
        } catch (Exception e) {
            log.error("粘贴标签接口 异常", e);
            return Result.error("出现未知错误，请联系管理员");
        }
        return ret ? Result.success() : Result.ok(false, "贴标签失败！");
    }
}
